package cn.pfz.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.pfz.Service.IBusinessService;
import cn.pfz.Service.IConsumptionItemService;
import cn.pfz.Service.IConsumptionService;
import cn.pfz.Service.ISystemDictionaryItemSerives;
import cn.pfz.domain.*;
import cn.pfz.query.ConsumptionQueryObject;
import cn.pfz.statusnum.ConsumptionStatusEnum;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Author: 普发忠
 * @Date: 2021/09/14/14:55
 */
@Controller
@RequestMapping("/consumption")
public class ConsumptionController {
    @Autowired
    private IConsumptionService consumptionService;
    @Autowired
    private IConsumptionItemService consumptionItemService;
    @Autowired
    private IBusinessService businessService;
    @Autowired
    private ISystemDictionaryItemSerives systemDictionaryItemSerives;
    
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") ConsumptionQueryObject qo) {
        List<Business> businesses = businessService.selectAll();
        model.addAttribute("businesses", businesses);
        ConsumptionStatusEnum[] values = ConsumptionStatusEnum.values();
        model.addAttribute("values", values);
        PageInfo<Consumption> list = consumptionService.list(qo);
        model.addAttribute("result", list);
        
        return "consumption/list";
    }
    
    
    @RequestMapping("/input")
    public String input(String cno, Model model) {
        //业务大类
        List<SystemDictionaryItem> dalei = systemDictionaryItemSerives.selectByTpyeid(1);
        //业务小类
        List<SystemDictionaryItem> xiaolei = systemDictionaryItemSerives.selectByTpyeid(2);
        //支付类型
        List<SystemDictionaryItem> zhifu = systemDictionaryItemSerives.selectByTpyeid(3);
        
        model.addAttribute("yewudalei", dalei);
        model.addAttribute("yewuxiaolei", xiaolei);
        model.addAttribute("zhifuleixing", zhifu);
        
        List<Business> businesses = businessService.selectAll();
        model.addAttribute("businesses", businesses);
        
        if (cno == null) {
            //新增
            
        } else {
            //编辑
            Consumption consumption = consumptionService.selectByCno(cno);
            model.addAttribute("currentConsumption", consumption);
            List<ConsumptionItem> consumptionItems = consumptionItemService.selectByCno(cno);
            model.addAttribute("consumptionItems", consumptionItems);
        }
        
        return "consumption/input";
    }
    
    @RequestMapping("/delete")
    public JsonResult delete(Long id) {
        consumptionService.deleteByPrimaryKey(id);
        return JsonResult.success(null);
    }
    
    @RequestMapping("saveOrUpdate")
    public String saveOrUpdate(Consumption consumption, Long[] category_id,
                               Long[] category_item_id, Long[] pay_type_id, @RequestParam("amount1") BigDecimal[] amount,
                               @RequestParam("discount_amount1") BigDecimal[] discount_amount, @RequestParam("pay_amount1") BigDecimal[] pay_amount) {
        
        if (consumption.getId()==null){
            String format = DateUtil.format(new Date(), "yyyyMMddHHmmss");
            String c = RandomUtil.randomNumbers(5);
            consumption.setCno(format+c);
            consumption.setStatus(0);
            consumption.setCreate_time(new Date());
            Subject subject = SecurityUtils.getSubject();
            consumption.setCreate_user((Employee) subject.getPrincipal());
            consumptionService.insert(consumption);
      
        
        
        //消费项目
        for (int i = 0; i <category_id.length;i++) {
            ConsumptionItem item = new ConsumptionItem(new SystemDictionaryItem(category_id[i]),
                    new SystemDictionaryItem(category_item_id[i]),
                    new SystemDictionaryItem(pay_type_id[i]),
                    amount[i], pay_amount[i], discount_amount[i]);
            item.setCreate_user(consumption.getCreate_user());
            item.setCreate_time(consumption.getCreate_time());
            item.setCno(consumption.getCno());
            consumptionItemService.insert(item);
        }
        
        }else {
            //根据表记录删除在增加
            consumptionService.updateByPrimaryKey(consumption);
            consumptionItemService.deleteByCno(consumption.getCno());
            for (int i=0; i<category_id.length; i++) {
                ConsumptionItem item = new ConsumptionItem(new SystemDictionaryItem(category_id[i]),
                        new SystemDictionaryItem(category_item_id[i]),
                        new SystemDictionaryItem(pay_type_id[i]),
                        amount[i], pay_amount[i], discount_amount[i]);
                item.setCreate_user(consumption.getCreate_user());
                item.setCreate_time(consumption.getCreate_time());
                item.setCno(consumption.getCno());
                consumptionItemService.insert(item);
            }
        }
    
        return "redirect:/consumption/list";
    }
    
}
